package com.fks.fks_service.controller;

import com.fks.fks_service.base.JsonResult;
import com.fks.fks_service.base.ResultCode;
import com.fks.fks_service.mapper.UserDBMapper;
import com.fks.fks_service.mapper.VerifyMapper;
import com.fks.fks_service.pojo.UserDB;
import com.fks.fks_service.utils.DateUtils;
import com.fks.fks_service.utils.JwtTokenUtil;
import com.fks.fks_service.utils.PrivacyEncryptionUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description LoginController
 * @Author mks
 * @Date 2024-12-27
 */
@RestController
@Transactional//开启事务回滚
@Api(tags = "登录注册模块")
@Slf4j
public class LoginController {

    @Autowired
    VerifyMapper verifyMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    UserDBMapper userDBMapper;

    /**
     * 电话+密码登录
     * @param request phone电话  password密码
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "电话",required = true,paramType = "param"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,paramType = "param")
    })
    @ApiOperation(value = "用户端电话+密码登录", notes = "用户登录")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功", response = UserDB.class)
    })
    @PostMapping("/api/login")
    public JsonResult login(HttpServletRequest request){
        //请求参数
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        if (phone == null || password == null) {
            return JsonResult.error(ResultCode.param_is_null,"缺少参数");
        }
        UserDB userDB = userDBMapper.selectUserByPhone(phone);
        if (userDB == null) {
            return JsonResult.error(ResultCode.user_not_find,"用户不存在");
        }
        log.info("login--userDB--"+userDB.getId()+"--password--"+userDB.getPassword());
        if (!passwordEncoder.matches(password,userDB.getPassword())){
            return JsonResult.error(ResultCode.password_error,"密码错误");
        }
        int userId = userDB.getId();
//        //生成token
//        String token = TokenUtils.makeToken();
//        //储存token
//        VerifyDB verifyDB = new VerifyDB();
//        verifyDB.setToken(token);
//        verifyDB.setUid(userDB.getId());
//        verifyDB.setTime(DateUtils.getNowTime());
//        //判断是否已经有记录
//        VerifyDB verifyDB1 = verifyDBMapper.getVerifyDBById(userDB.getId());
//        if (verifyDB1==null){
//            verifyDBMapper.insertToken(verifyDB);
//        }else {
//            verifyDBMapper.updateToken(verifyDB);
//        }

        //生成token
        String token = JwtTokenUtil.createJwtToken(String.valueOf(userId),"user");
        String jwtRefreshToken = JwtTokenUtil.createJwtRefreshToken(String.valueOf(userId),"user");

        //返回token
        userDB.setToken(token);
        userDB.setJwtRefreshToken(jwtRefreshToken);

        //判断是否存在token记录
        String verify = verifyMapper.getVerifyDBById(userId,"0");
        if (verify==null){
            //生成新的token记录
            verifyMapper.saveToken(userId,token, DateUtils.getNowTime(),"0");
        }else {
            verifyMapper.updateToken(userId,token, DateUtils.getNowTime(),"0");
        }

        //私密信息加密
        userDB = new PrivacyEncryptionUtil().privacyEncryption(userDB);

        return JsonResult.success("登录成功").data(userDB);
    }
}
